#include "Datetime.h"

Datetime::Datetime()
{
    this->n_dia = "";
    this->n_mes = "";
    this->dia = 0;
    this->mes = 0;
    this->ano = 0;
    this->hor = 0;
    this->min = 0;
    this->seg = 0;
}

string Datetime::nomeDia(int dia)
{
    switch(dia)
    {
    case 0 :
        return "Dom";
        break;
    case 1 :
        return "Seg";
        break;
    case 2 :
        return "Ter";
        break;
    case 3 :
        return "Qua";
        break;
    case 4 :
        return "Qui";
        break;
    case 5 :
        return "Sex";
        break;
    case 6 :
        return "Sab";
        break;
    default:
        return "";
        break;
    }
    return "";
}

string Datetime::nomeMes(int mes)
{
    switch (mes)
    {
    case 1 :
        return "Jan";
        break;
    case 2 :
        return "Fev";
        break;
    case 3 :
        return "Mar";
        break;
    case 4 :
        return "Abr";
        break;
    case 5 :
        return "Mai";
        break;
    case 6 :
        return "Jun";
        break;
    case 7 :
        return "Jul";
        break;
    case 8 :
        return "Ago";
        break;
    case 9 :
        return "Set";
        break;
    case 10 :
        return "Out";
        break;
    case 11 :
        return "Nov";
        break;
    case 12 :
        return "Dez";
        break;
    default:
        return "";
        break;
    }
    return "";
}

void Datetime::set_n_dia(int dia)
{
    this->n_dia = this->nomeDia(dia);
}

void Datetime::set_n_mes(int mes)
{
    this->n_mes = this->nomeMes(mes);
}

void Datetime::set_Date(int D, int M, int Y)
{
    if ((D>0)&&(D<32)&&(M>0)&&(M<13)&&(Y>1900))
    {
        this->dia = D;
        this->mes = M;
        this->ano = Y;
    }
}

void Datetime::set_Time(int h, int m, int s)
{
    if ((h>=0)&&(h<24)&&(m>=0)&&(m<60)&&(s>=0)&&(s<60))
    {
        this->hor = h;
        this->min = m;
        this->seg = s;
    }
}

void Datetime::set_Datetime(int D, int M, int Y, int h, int m, int s)
{
    if ((D>0)&&(D<32)&&(M>0)&&(M<13)&&(Y>1900)&&
            (h>=0)&&(h<24)&&(m>=0)&&(m<60)&&(s>=0)&&(s<60))
    {
        this->dia = D;
        this->mes = M;
        this->ano = Y;
        this->hor = h;
        this->min = m;
        this->seg = s;
    }
}

void Datetime::set_Datetime(string AAAA_MM_DD_hh_mm_ss)
{
    string datetime = AAAA_MM_DD_hh_mm_ss;
    string ano,mes,dia;
    string hor,min,seg;

    ano = datetime.substr(0,4);
    mes = datetime.substr(5,2);
    dia = datetime.substr(8,2);

    hor = datetime.substr(11,2);
    min = datetime.substr(14,2);
    seg = datetime.substr(17,2);

    this->ano = fromString<int>(ano);
    this->mes = fromString<int>(mes);
    this->dia = fromString<int>(dia);

    this->hor = fromString<int>(hor);
    this->min = fromString<int>(min);
    this->seg = fromString<int>(seg);
}

void Datetime::set_localtime()
{

    struct tm *local;
    time_t t;

    t = time(NULL);
    local = localtime(&t);

    this->dia = local->tm_mday;
    this->mes = local->tm_mon + 1;
    this->ano = local->tm_year;
    this->ano += 1900;

    this->n_dia = this->nomeDia(local->tm_wday + 1);
    this->n_mes = this->nomeMes(this->mes);

    this->hor = local->tm_hour;
    this->min = local->tm_min;
    this->seg = local->tm_sec;
}

void Datetime::cleartime()
{
    this->n_dia = "";
    this->n_mes = "";
    this->dia = 0;
    this->mes = 0;
    this->ano = 0;
    this->hor = 0;
    this->min = 0;
    this->seg = 0;
}

string Datetime::get_Date()
{
    string D,M,A;

    if (this->dia<10)
        D = "0";
    D += toString(this->dia);
    if (this->mes<10)
        M = "0";
    M += toString(this->mes);
    A = toString(this->ano);

    return A + _SPACER_DATE_ + M + _SPACER_DATE_ + D;
}

string Datetime::get_Time()
{
    string h,m,s;

    if (this->hor<10)
        h = "0";
    h += toString(this->hor);
    if (this->min<10)
        m = "0";
    m += toString(this->min);
    if (this->seg<10)
        s = "0";
    s += toString(this->seg);

    return h + _SPACER_TIME_ + m + _SPACER_TIME_ + s;
}

string Datetime::get_Datetime()
{
    return this->get_Date() + " " + this->get_Time();
}

string Datetime::get_n_dia()
{
    return this->n_dia;
}

string Datetime::get_n_mes()
{
    return this->n_mes;
}

